package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.CampProm;
import com.telecom.cms.util.Constant;
import com.telecom.cms.util.CurrentTime;

public class CampPromDAO {
	Dao dao = new DaoImp();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampProm transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Camp_Prom where prom_id="
				+ transientInstance.getPromId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Camp_Prom set latn_id='"
						+ transientInstance.getLatnId() + "',prom_name='"
						+ transientInstance.getPromName() + "',prom_desc='"
						+ transientInstance.getPromDesc() + "',prom_pic='"
						+ transientInstance.getPromPic() + "',vaild_month='"
						+ transientInstance.getVaildMonth()
						+ "',Creator_Name='"
						+ transientInstance.getCreatorName() + "',Create_Time=timestamp '"
						+ CurrentTime.getCurrentTime()
						+ "' where prom_id="
						+ transientInstance.getPromId();
			else
				sql = "insert into " + db + ".Camp_Prom values('"
						+ transientInstance.getPromId() + "','"
						+ transientInstance.getLatnId() + "','"
						+ transientInstance.getPromName() + "','"
						+ transientInstance.getPromDesc() + "','"
						+ transientInstance.getPromPic() + "','"
						+ transientInstance.getVaildMonth() + "','"
						+ transientInstance.getCreatorName() + "',timestamp '"
						+ CurrentTime.getCurrentTime() + "')";
			rs = dao.insert(sql);
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(CampProm persistentInstance) {

		String sql = "delete from " + db + ".Camp_Prom where Prom_Id = "
				+ persistentInstance.getPromId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public CampProm findById(String id) {

		String sql = "select * from " + db + ".Camp_Prom where Prom_Id = " + id;
		CampProm sd = new CampProm();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setPromId(rs.getInt(1));
				sd.setLatnId(rs.getString(2));
				sd.setPromName(rs.getString(3));
				sd.setPromDesc(rs.getString(4));
				sd.setPromPic(rs.getString(5));
				sd.setVaildMonth(rs.getInt(6));
				sd.setCreatorName(rs.getString(7));
				sd.setCreateTime(rs.getString(8));
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return sd;

	}

	public List findAll(String latnid) {

		String sql = "select * from " + db + ".Camp_Prom where latn_id='"+latnid+"'";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampProm sd = new CampProm();
				sd.setPromId(rs.getInt(1));
				sd.setLatnId(rs.getString(2));
				sd.setPromName(rs.getString(3));
				sd.setPromDesc(rs.getString(4));
				sd.setPromPic(rs.getString(5));
				sd.setVaildMonth(rs.getInt(6));
				sd.setCreatorName(rs.getString(7));
				sd.setCreateTime(rs.getString(8));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

}
